﻿@model Cxuu.Models.EditExtVideo
<div class="content-top-nav">
    <div class="layui-row">
        <div class="layui-col-md11">
            <h3 style="color:#444">
                <i class="layui-icon layui-icon-home"></i>
                视频编辑
            </h3>
        </div>
        <div class="layui-col-md1" style="text-align:right;">
            <button class="layui-btn layui-btn-sm" onclick="loadMain('/Admin/Extvideo/')">返回列表</button>
        </div>
    </div>
    <hr>
</div>

<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-form">
        <form class="layui-form" action="" lay-filter="FormEdit" enctype="multipart/form-data">
            <div class="layui-form-item">
                <label asp-for="Cid" class="layui-form-label required"></label>
                <div class="layui-input-inline">
                    <select asp-for="Cid" asp-items="ViewBag.articleCateData"></select>
                </div>
            </div>
            <div class="layui-form-item">
                <label asp-for="Title" class="layui-form-label required"></label>
                <div class="layui-input-block">
                    <input type="text" asp-for="Title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label asp-for="Content" class="layui-form-label"></label>
                <div class="layui-input-block">
                    <textarea type="text"  id="ContentEdit" asp-for="Content" required lay-verify="required" placeholder="请输入内容"
                              autocomplete="off" class="layui-input"></textarea>
                </div>
            </div>
            @if (CheckExt.AuthView(ViewBag.sr, "extvideo_att"))
            {
                <div class="layui-form-item">
                    <label class="layui-form-label required">位置属性</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="AttA" value="1" title="头条">
                        <input type="checkbox" name="AttB" value="1" title="小头条">
                        <span id="lunhuantu"><input type="checkbox" name="AttC" value="1" title="图片轮换"></span>
                    </div>
                </div>
            }

            <div class="layui-form-item">
                <label asp-for="Examine" class="layui-form-label required"></label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Examine" lay-verify="required" lay-reqtext="发布人不能为空" placeholder="请输入发布人" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Img" class="layui-form-label required"></label>
                <div class="layui-input-block">
                    <input type="text" id="imageval" asp-for="Img" value="" class="layui-input" lay-verify="required" lay-reqtext="缩略图不能为空">
                    <button type="button" class="layui-btn" id="thumimg">
                      <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                </div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Status" class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    @if (CheckExt.AuthView(ViewBag.sr, "extvideo_status"))
                    {
                        <input asp-for="Status" type="radio" value="1" title="发布" checked>
                    }
                    <input asp-for="Status" type="radio" value="0" title="草稿">
                    <input asp-for="Id" type="hidden">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn" id="saveBtn">保存</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'upload','jquery', 'element','layedit'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery,upload = layui.upload, element = layui.element,layedit = layui.layedit;
        var article = layedit.build('ContentEdit',{
             tool: [
               'strong' //加粗
               ,'italic' //斜体
               ,'underline' //下划线
               ,'del' //删除线
 
               ,'|' //分割线
               ,'left' //左对齐
               ,'center' //居中对齐
               ,'right' //右对齐
               ,'link' //超链接
               ,'unlink' //清除链接
             ]
             ,height: 280
         });

        form.render();

        

        var editId = @ViewBag.id+1;
        //console.log(editId);
        if (editId > 1) {
            $.getJSON("/Admin/ExtVideo/AddOrEdit?id=@ViewBag.id&edit=1", function (res) {
                if (res.status == 1) {
                    form.val('FormEdit', {
                        Cid: res.data.cid,
                        //Content: res.data.content,
                        Examine: res.data.examine,
                        Attments: res.data.attments,
                        Img: res.data.img,
                        Id: res.data.id,
                        Status: res.data.status,
                        AttA: res.data.attA,
                        AttB: res.data.attB,
                        AttC: res.data.attC,
                        Time: res.data.time,
                        Title: res.data.title,
                    });
                    if (res.data.status)
                        $('input[name="Status"][value="1"]').prop('checked', 'checked');
                    else
                        $('input[name="Status"][value="0"]').prop('checked', 'checked');
                    form.render('radio');

                    if (res.data.attA ==1)
                        $('input[name="AttA"]').prop('checked', 'checked');
                    if (res.data.attB == 1)
                        $('input[name="AttB"]').prop('checked', 'checked');
                    if (res.data.attC == 1)
                        $('input[name="AttC"]').prop('checked', 'checked');
                    form.render('checkbox');
                    layedit.setContent(article, res.data.content, false);//编辑器赋值
                }
                //console.log("json", res+"abc");

                //如果没有提取缩略图，就不能选择 轮换图
                var imageval = $('#imageval').val();
                if (imageval.length < 1) {
                    $('#lunhuantu').hide();
                } else {
                    $('#lunhuantu').show();
                }
            })
        } else {
            form.val('FormEdit', {
                Cid: @ViewBag.cid,
            });
            $('input[name="Status"][value="true"]').prop('checked', 'checked');
            form.render('radio');
        }
        //检测缩略图，显示隐藏
        $('#imageval').bind('input propertychange', function () {
            if ($(this).val().length) {
                $('#lunhuantu').show();
            } else {
                $('#lunhuantu').hide();
            }

        });



        var uploadInst = upload.render({
            elem: '#thumimg'
            , url: '/Admin/Upload/UploadingStream?type=layuiImg&source=ExtVedio&uid=@ViewBag.uid'
            , before: function (obj) {
                element.progress('upload-att', '0%'); //进度条复位
                layer.msg('上传中', { icon: 16, time: 0 });
            }
            , done: function (res) {
                if (res.status == 1) {
                     $('#imageval').val(res.data.url);
                    layer.msg(res.info);
                } else {
                    layer.msg("上传失败！");
                }
            }
            //进度条
            , progress: function (n, elem, e) {
                element.progress('upload-att', n + '%');
                if (n == 100) {
                    layer.msg('上传完毕', { icon: 1 });
                }
            }
        });
        var dataPost = function (url, data) {
            console.log("json", data);
            $.post(url, data, function (res) {
                if (res.status == 1) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        loadMain('/Admin/Extvideo/Index');
                    });
                } else
                    layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
            });
        }

        //监听提交
        //form.on('submit(saveBtn)', function (data) {  form.val('FormEdit')
        $('#saveBtn').click(function () {
            //var field = $("form").serializeArray(); //输出数组
           var field = form.val('FormEdit');
            if (editId == 1) {
                var post = {
                    Cid: field.Cid,
                    Title: field.Title,
                    Examine: field.Examine,
                    Img: field.Img,
                    Content: layedit.getContent(article),
                    Status: field.Status,
                    AttA: field.AttA,
                    AttB: field.AttB,
                    AttC: field.AttC
                };
                dataPost('/Admin/Extvideo/CreatePost', post);
            }
            else {
                var postEdit = {
                    Id: field.Id,
                    Cid: field.Cid,
                    Title: field.Title,
                    Examine: field.Examine,
                    Img: field.Img,
                    Content: layedit.getContent(article),
                    Status: field.Status,
                    AttA: field.AttA == 1 ? 1 : 0,
                    AttB: field.AttB == 1 ? 1 : 0,
                    AttC: field.AttC == 1 ? 1 : 0
                };
                dataPost('/Admin/Extvideo/EditPost', postEdit);
            }
            return false;
        });
    });
</script>